package com.niodata.dp.task.service.impl;

import com.niodata.dp.task.dao.PluginPackageMapper2;
import com.niodata.dp.task.entity.PluginCategoryCountSta;
import com.niodata.dp.task.entity.PluginDocument;
import com.niodata.dp.task.entity.PluginDocumentView;
import com.niodata.dp.task.entity.PluginPackage;
import com.niodata.dp.task.entity.PluginPackageView;
import com.niodata.dp.task.service.PluginHubManager;
import com.niodata.dp.task.service.PluginManager2;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
@Qualifier("PluginHubManagerImpl")
public class PluginHubManagerImpl implements PluginHubManager {

  @Autowired
  @Qualifier("PluginManager2Impl")
  private PluginManager2 pluginManager2;

  @Autowired
  private PluginPackageMapper2 pluginPackageMapper;

  @Override
  public PluginCategoryCountSta getPluginCategoryCount() {

    return pluginManager2.getPublicToHubPluginCategoryCount();
  }

  @Override
  public List<PluginPackageView> getPlugins(String sysCategory, String funcCategory,
      String nameContains, int offset, int maxCount) {
    return pluginManager2.getPluginsPlublicToHub(
        sysCategory,
        funcCategory,
        nameContains,
        offset,
        maxCount);
  }


  @Override
  public PluginDocument getPluginDocument(String name, String version) {
    return pluginManager2.getPluginDocument(0, name, version);
  }

  @Override
  public PluginDocumentView getPluginDocumentView(String name, String version) {
    return pluginManager2.getPluginDocumentView(0, name, version);
  }

  @Override
  public PluginDocumentView getPluginDocumentView(String pkgId) {
    return pluginManager2.getPluginDocumentView(0, pkgId);
  }

  @Override
  public synchronized void incrDownTimes(String name, String version, int increment) {
    PluginPackage pluginPackage =
        pluginManager2.getPluginByNameVersion(0, name, version);
    pluginPackageMapper.updateDownTimes(name, version,
        pluginPackage.getDownTimes() + increment);
  }
}
